package ldk.apps.adbconnections.handlers

import com.google.gson.JsonParser
import ldk.apps.adbconnections.MainServer
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import kotlin.reflect.KType
import kotlin.reflect.full.createType

/**
 * Created by ldkxingzhe@163.com on 2018/2/5.
 */
class BaseHandlerTest{

    private lateinit var baseHandler: BaseHandler

    @Before
    fun setUp(){
        baseHandler = object : BaseHandler() {
            override val ACTION: Short = 0x45

            fun action(type: Int, name: String){
                assertEquals(100, type)
                assertEquals("GOOD", name)
            }
        }
    }

    @Test
    fun testConvert(){
        val jsonString = "{\"num\":100,\"name\":\"世界很大\"}"
        val jsonObject = JsonParser().parse(jsonString).asJsonObject
        val numValue = baseHandler.convert(jsonObject["num"], Int::class.createType()) as Int
        val nameValue = baseHandler.convert(jsonObject["name"], String::class.createType()) as String
        assertEquals(100, numValue)
        assertEquals("世界很大", nameValue)
    }

    @Test
    fun testHandle(){
        val jsonString = "{\"type\":100,\"name\":\"GOOD\"}"
        baseHandler.handle(null, 10, jsonString.toByteArray(Charsets.UTF_8))
    }
}